package main

import (
	"fmt"
	"sync"
	"time"
)

var wg1 sync.WaitGroup

func main() {
	// 声明,定义一个管道
	var intChan chan int
	intChan = make(chan int, 10)
	wg1.Add(2)
	go readChan(intChan)
	go writeChan(intChan)
	wg1.Wait()
	fmt.Println("读写结束")
}

func writeChan(intChan chan int) {
	defer wg1.Done()
	for i := 0; i < 50; i++ {
		intChan <- i
		fmt.Println("存入：", i)
		time.Sleep(time.Second)
	}
	close(intChan)
}

func readChan(intChan chan int) {
	defer wg1.Done()
	for i := range intChan { // 管道会阻塞读取
		fmt.Println("读取", i)
	}
}
